const fs = require('fs');
const path = require('path')

function includesUppercase(str) {
  return /[A-Z]/.test(str);
}
function includesDashOrUnderscore(str) {
  return /-|_/.test(str);
}
function tagReadString(str) {
  //1.假如输入的名称是mycomponent
  //includesUppercase判断是否含有大写字母 includesDashOrUnderscore是否含有下划线_
  if (!includesUppercase(str) && !includesDashOrUnderscore(str)) {
    console.log('输入的名称格式不正确，请确认是否包含大写字母和下划线');
    process.exit(1)
  }
  //2.输入格式包含-或者_
  if (includesDashOrUnderscore(str)) {
    return str.split('_').join('-').toLowerCase();
  }
  //3.其他格式 myText / MyTest / MyShinyTest => my-shiny-test
  str = str.replace(/[A-Z]/g, match => {
    //MyTest => My-Test
    return "-" + match;
  })
  if (str.startsWith('-')) {
    str = str.substring(1);
  }
  return str.toLowerCase();

}

module.exports = async function getComponent(name, des) {
  console.log(name, des);
  //1.读取模版
  fs.readFile(path.join(__dirname, '../templates', 'tmp.js'), 'utf-8', (err, data) => {
    if (err) throw err;
    // console.log(data);
    //2.没有components文件夹则创建
    if (!fs.existsSync(des)) {
      fs.mkdir(des, err => {
        if (err) throw err;
        console.log(`创建了一个文件夹${des}`);
      })
    }

    //3.读取代码 替换name
    let filename;
    if (name.includes('.')) {
      filename = name.split('.')[0];
    } else {
      filename = name;
    }
    const tagName = tagReadString(filename);


  })




  //4.在components文件夹里面创建 一个新的name模版

}